package org.qpcr.community.device.web;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.web.authorization.annotation.Authorize;
import org.hswebframework.web.authorization.annotation.Resource;
import org.hswebframework.web.crud.web.reactive.ReactiveServiceCrudController;
import org.qpcr.community.device.entity.CalibrationExperimentalRecordsEntity;
import org.qpcr.community.device.service.CalibrationExperimentalRecordsService;
import org.qpcr.community.device.web.request.GetFluorescenceCalibrationRequest;
import org.qpcr.community.device.web.response.GetFluorescenceCalibrationResponse;
import org.springframework.http.MediaType;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping({"/device/calibration-experimental-records", "/device/calibration-experimental/records"})
@Authorize(ignore = true)
@Resource(id = "calibration-experimental-records-instance", name = "荧光校准实验记录")
@Slf4j
@Tag(name = "实验记录接口")
@RequiredArgsConstructor
public class CalibrationExperimentalRecordsController   implements ReactiveServiceCrudController<CalibrationExperimentalRecordsEntity, String> {
    @Getter
    private final CalibrationExperimentalRecordsService service;


    @PostMapping(value = "/parsing-excel", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Authorize(ignore = true)
    @Operation(summary = "上传实验数据Excel文件")
    public Mono<Integer> parsingExcel(@RequestPart("file") FilePart filePart, String deviceId, String moduleCode,@RequestParam(defaultValue = "实验记录") String projectName,Long receiveTime) {
        return service.parseExcelToExperimentalRecords(filePart,deviceId,moduleCode,projectName,receiveTime)
                      .doOnError(error -> log.error("解析Excel文件失败", error));
    }


    @PostMapping("/get-fluorescence-calibration-param")
    @Authorize(ignore = true)
    @Operation(summary = "获取荧光校准参数")
    public Mono<GetFluorescenceCalibrationResponse> getFluorescenceCalibrationParam(@RequestBody GetFluorescenceCalibrationRequest req) {
        return service.getFluorescenceCalibrationParam(req);
    }


    @PostMapping("/clear/{deviceId}/{moduleCode}")
    @Authorize(ignore = true)
    @Operation(summary = "清空校准实验记录")
    public Mono<Integer> clear(@PathVariable String deviceId,@PathVariable String moduleCode) {
        return service.clear(deviceId,moduleCode);
    }
}
